﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Xml.Linq;
using System.Web.Services;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_KPI : System.Web.UI.Page
    {

        private IProject projectBLL;
        private SystemTypeYearBLL sysTypeYearBLL = new SystemTypeYearBLL();
        private ComboBLL comboBLL = new ComboBLL();
        private string type;
        string[] strMon = { "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค." };
        string[] strMonth = { "มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม" };

        protected void Page_Load(object sender, EventArgs e)
        {
            type = Request.QueryString["type"].ToString();

            if (type.Length > 0)
                projectBLL = Project_Factory.GetProjectFactory(type);

            if (!IsPostBack)
            {
                if (!Object.Equals(Session["ProjectID"], null))
                {
                    lbProjectID.Text = Session["ProjectID"].ToString();
                }

                if (!Object.Equals(Session["ProjectName"], null))
                {
                    litTitle.Text = Session["ProjectName"].ToString();
                }

                loadKpiType();
                LoadData();
            }
            //LoadData();
        }

        private void LoadData()
        {
            DataRow dr = sysTypeYearBLL.GetDataSystemTypeYear().Rows[0];
            lblMonthType.Text = dr["month"].ToString();

            DataTable dt = projectBLL.GetDataProjectKPI(lbProjectID.Text);
            Grid_t9.DataSource = dt;
            Grid_t9.DataBind();
        }

        private void loadKpiType()
        {
            DataTable info = comboBLL.ListKpiType();
            ddlKpiType_t9.DataSource = info;
            ddlKpiType_t9.DataBind();
        }

        private void clear_t9()
        {
            txtName_t9.Text = "";
            txtDescr_t9.Text = "";
            txtUnit_t9.Text = "";
            txtWeight_t9.Text = "";

            txtScore1_t9.Text = "";
            txtScore2_t9.Text = "";
            txtScore3_t9.Text = "";
            txtScore4_t9.Text = "";
            txtScore5_t9.Text = "";

            txtTragetM1_t9.Text = "";
            txtTragetM2_t9.Text = "";
            txtTragetM3_t9.Text = "";
            txtTragetM4_t9.Text = "";
            txtTragetM5_t9.Text = "";
            txtTragetM6_t9.Text = "";
            txtTragetM7_t9.Text = "";
            txtTragetM8_t9.Text = "";
            txtTragetM9_t9.Text = "";
            txtTragetM10_t9.Text = "";
            txtTragetM11_t9.Text = "";
            txtTragetM12_t9.Text = "";

            txtMTEF1_t9.Text = "";
            txtMTEF2_t9.Text = "";
            txtMTEF3_t9.Text = "";
        }
        private void bindTab9_forEdit(string seq)
        {
            lblSeq_t9.Text = seq;

            DataRow dr = projectBLL.GetDataProjectKPIBySequence(seq).Rows[0];

            txtName_t9.Text = dr["bgm_prj_kpi_name"].ToString();
            txtDescr_t9.Text = dr["bgm_prj_kpi_descr"].ToString();
            txtUnit_t9.Text = dr["unit"].ToString();
            txtWeight_t9.Text = dr["bgm_prj_kpi_weight"].ToString();
            txtScore1_t9.Text = dr["score_1"].ToString();
            txtScore2_t9.Text = dr["score_2"].ToString();
            txtScore3_t9.Text = dr["score_3"].ToString();
            txtScore4_t9.Text = dr["score_4"].ToString();
            txtScore5_t9.Text = dr["score_5"].ToString();
            txtTragetM1_t9.Text = dr["target_m1"].ToString();
            txtTragetM2_t9.Text = dr["target_m2"].ToString();
            txtTragetM3_t9.Text = dr["target_m3"].ToString();
            txtTragetM4_t9.Text = dr["target_m4"].ToString();
            txtTragetM5_t9.Text = dr["target_m5"].ToString();
            txtTragetM6_t9.Text = dr["target_m6"].ToString();
            txtTragetM7_t9.Text = dr["target_m7"].ToString();
            txtTragetM8_t9.Text = dr["target_m8"].ToString();
            txtTragetM9_t9.Text = dr["target_m9"].ToString();
            txtTragetM10_t9.Text = dr["target_m10"].ToString();
            txtTragetM11_t9.Text = dr["target_m11"].ToString();
            txtTragetM12_t9.Text = dr["target_m12"].ToString();
            txtMTEF1_t9.Text = dr["mtef_1"].ToString();
            txtMTEF2_t9.Text = dr["mtef_2"].ToString();
            txtMTEF3_t9.Text = dr["mtef_3"].ToString();
            ddlKpiType_t9.SelectedValue = dr["bgm_kpi_type_seq"].ToString();
            ddlTarket_t9.SelectedValue = dr["target_type"].ToString();
        }

        //---- event control
        protected void Grid_t9_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetInsertClick")
            {
                PanelAddEdit_t9.Visible = true;
                clear_t9();
                btnSave_t9.Visible = true;
                btnEdit_t9.Visible = false;
            }
            else if (e.CommandName == RadGrid.EditCommandName)
            {
                PanelAddEdit_t9.Visible = true;
                clear_t9();
                btnSave_t9.Visible = false;
                btnEdit_t9.Visible = true;
                bindTab9_forEdit((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString());

            }
            else if (e.CommandName == "Delete")
            {
                int nRowSelected = 0;
                foreach (GridDataItem data in Grid_t9.MasterTableView.GetSelectedItems())
                {
                    projectBLL.DeleteProjectKPI(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"].ToString());
                    nRowSelected++;
                }
                PanelAddEdit_t9.Visible = false;
            }
            else if (e.CommandName == RadGrid.ExpandCollapseCommandName)
            {
                string key_grid1 = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();
                DataTable dt = projectBLL.GetDataProjectKPIBySequence(key_grid1);

                RadGrid grdIn_t9 = (RadGrid)((GridDataItem)e.Item).ChildItem.FindControl("GridIn_t9");
                grdIn_t9.DataSource = dt;
                grdIn_t9.DataBind();
                return;
            }

            else
            { 
            
            }

            LoadData();
        }

        protected void Grid_t9_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                int monType = int.Parse(lblMonthType.Text);
                int tmp;
                GridHeaderItem header = (GridHeaderItem)e.Item;
                string strColumn;
                for (int i = 0; i < 12; i++)
                {
                    tmp = i + monType;
                    if (tmp > 12)
                        tmp = tmp - 12;
                    strColumn = "target_m" + (i + 1).ToString();
                    header[strColumn].Text = strMonth[tmp - 1];

                    Label lbl = (Label)PanelAddEdit_t9.FindControl("lblMonth" + (i + 1).ToString());
                    lbl.Text = strMonth[tmp - 1] + ":";
                }
            }
        }

        protected void btnCancel_t9_Click(object sender, EventArgs e)
        {
            PanelAddEdit_t9.Visible = false;
            clear_t9();
            LoadData();
        }

        protected void btnSave_t9_Click(object sender, EventArgs e)
        {
            txtMTEF1_t9.Text = txtMTEF1_t9.Text != "" ? txtMTEF1_t9.Text : "0";
            txtMTEF2_t9.Text = txtMTEF2_t9.Text != "" ? txtMTEF2_t9.Text : "0";
            txtMTEF3_t9.Text = txtMTEF3_t9.Text != "" ? txtMTEF3_t9.Text : "0";

            try
            {
                projectBLL.InsertProjectKPI(txtName_t9.Text, txtDescr_t9.Text, txtUnit_t9.Text, txtWeight_t9.Text, lbProjectID.Text, ddlKpiType_t9.SelectedValue, txtScore1_t9.Text, txtScore2_t9.Text, txtScore3_t9.Text, txtScore4_t9.Text, txtScore5_t9.Text, ddlTarket_t9.SelectedValue, txtTragetM1_t9.Text, txtTragetM2_t9.Text, txtTragetM3_t9.Text, txtTragetM4_t9.Text, txtTragetM5_t9.Text, txtTragetM6_t9.Text, txtTragetM7_t9.Text, txtTragetM8_t9.Text, txtTragetM9_t9.Text, txtTragetM10_t9.Text, txtTragetM11_t9.Text, txtTragetM12_t9.Text, txtMTEF1_t9.Text, txtMTEF2_t9.Text, txtMTEF3_t9.Text, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('บันทึกเสร็จเรียบร้อย');", true);
            }
            catch
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('เกิดข้อผิดพลาดในการบันทึก');", true);
            }

            LoadData();
            clear_t9();
            PanelAddEdit_t9.Visible = false;
        }

        protected void btnEdit_t9_Click(object sender, EventArgs e)
        {
            txtMTEF1_t9.Text = txtMTEF1_t9.Text != "" ? txtMTEF1_t9.Text : "0";
            txtMTEF2_t9.Text = txtMTEF2_t9.Text != "" ? txtMTEF2_t9.Text : "0";
            txtMTEF3_t9.Text = txtMTEF3_t9.Text != "" ? txtMTEF3_t9.Text : "0";
            try
            {
                projectBLL.UpdateProjectKPI(lblSeq_t9.Text, txtName_t9.Text, txtDescr_t9.Text, txtUnit_t9.Text, txtWeight_t9.Text, ddlKpiType_t9.SelectedValue, txtScore1_t9.Text, txtScore2_t9.Text, txtScore3_t9.Text, txtScore4_t9.Text, txtScore5_t9.Text, ddlTarket_t9.SelectedValue, txtTragetM1_t9.Text, txtTragetM2_t9.Text, txtTragetM3_t9.Text, txtTragetM4_t9.Text, txtTragetM5_t9.Text, txtTragetM6_t9.Text, txtTragetM7_t9.Text, txtTragetM8_t9.Text, txtTragetM9_t9.Text, txtTragetM10_t9.Text, txtTragetM11_t9.Text, txtTragetM12_t9.Text, txtMTEF1_t9.Text, txtMTEF2_t9.Text, txtMTEF3_t9.Text, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('บันทึกเสร็จเรียบร้อย');", true);
            }
            catch
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('เกิดข้อผิดพลาดในการบันทึก');", true);
            }
            
            LoadData();
            clear_t9();
            PanelAddEdit_t9.Visible = false;
        }

        protected void Grid_t9_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem commandItem = e.Item as GridCommandItem;
                LinkButton button = commandItem.FindControl("AddNew_t5") as LinkButton;
                LinkButton buttonDel = commandItem.FindControl("Delete_t5") as LinkButton;
                if ((bool)Session["Approve"])
                {
                    button.Enabled = false;
                    button.Visible = true;
                    button.Text = "";

                    buttonDel.Enabled = false;
                    buttonDel.Visible = true;
                    buttonDel.Text = "";
                }
            }
        }
    }
}
